package cc.rengu.redp.bizimpl.igas.controller;


import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.igas.domain.PBankSupportCfg;
import cc.rengu.redp.bizimpl.igas.domain.PDstChannelInfo;
import cc.rengu.redp.bizimpl.igas.service.PBankSupportCfgService;
import cc.rengu.redp.bizimpl.upmp.domain.BankCodeInfo;
import cc.rengu.redp.bizimpl.upmp.service.BankCodeInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.bizimpl.utils.RedpUpmpUtil;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘玉亮.LL
 */
@Slf4j
@Api(tags = "支持银行列表配置管理")
@RestController
@RequestMapping("/admin/igas/pBankSupportCfg")
public class PBankSupportCfgController extends BaseController {
    @Autowired
    private PBankSupportCfgService pBankSupportCfgService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private  BankCodeInfoService bankCodeInfoService;
    @GetMapping(value = "")

    public Map<String, Object> list(QueryRequest request, PBankSupportCfg entity) {
        return getDataTable(this.pBankSupportCfgService.findByPage(request, entity));
    }

    @GetMapping(value = "/editFromChnl")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "操作类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public PBankSupportCfg editFromChnl(PDstChannelInfo pDstChannelInfo) throws RedpException {
        return pBankSupportCfgService.editFromChnl(pDstChannelInfo);
    }
    @GetMapping("getOne")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dstChannelId", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "supportBizType", value = "支持业务类别", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bankNo", value = "银行机构号", required = true, dataType = "String")
    })
    public PBankSupportCfg getOne(PBankSupportCfg entity) throws RedpException {
        return pBankSupportCfgService.findOne(entity.getInstId(),entity.getDstChannelId(),entity.getSupportBizType(),entity.getBankNo());
    }
    @PostMapping(value = "/edit")
    public void editFromChnl(@ModelAttribute("pSecPlanInfo") PBankSupportCfg entity) throws RedpException {
        PBankSupportCfg pBankSupportCfg = pBankSupportCfgService.findOne(entity.getInstId(), entity.getDstChannelId(), entity.getSupportBizType(), entity.getBankNo());
        if (Common.isNotBlank(pBankSupportCfg)) {
            BeanUtils.copyProperties(entity, pBankSupportCfg);
            LambdaUpdateWrapper<PBankSupportCfg> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PBankSupportCfg::getInstId, entity.getInstId());
            wrapper.eq(PBankSupportCfg::getDstChannelId, entity.getDstChannelId());
            wrapper.eq(PBankSupportCfg::getSupportBizType, entity.getSupportBizType());
            wrapper.eq(PBankSupportCfg::getBankNo, entity.getBankNo());
            pBankSupportCfgService.update(pBankSupportCfg,wrapper);
        } else {
            QueryWrapper<BankCodeInfo> wrapper=new QueryWrapper<>();
            wrapper.eq("Bank_Branch_Code", entity.getBankNo());

            wrapper.eq("Bank_Branch_Level", "00");
            UpmpUserInfoVO user = RedpUpmpUtil.getCurrentUser(entity.getInstId());
            entity.setOprId(user.getOprId());
            List<BankCodeInfo> list = bankCodeInfoService.list(wrapper);
            if (!list.isEmpty()){
                entity.setBankName(list.get(0).getBankName());
                pBankSupportCfgService.save(entity);
            }else {
                log.info("未查到银行数据，请重新选择");
            }

        }
    }

    @PostMapping(value = "/changeStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dstChannelId", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public void changeStatus(PBankSupportCfg entity) throws RedpException {

        PBankSupportCfg db = pBankSupportCfgService.findOne(entity.getInstId(),entity.getDstChannelId(),entity.getSupportBizType(),entity.getBankNo());;
        if (Common.isBlank(db)) {
            log.error("根据唯一索引没有找到指定数据！");
            throw new RedpException("没有找到指定数据！");
        }
        if(IgasYesOrNoEnum.Yes.getType().equals(entity.getRecordStatus())
                ||IgasYesOrNoEnum.No.getType().equals(entity.getRecordStatus())){

            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            db.setRecordStatus(entity.getRecordStatus());
            db.setLastOprId(user.getId());
            db.setLastUpdateTime(DateUtils.formatDate(new Date(),DateUtils.DEFAULT_TIME_FORMAT+".SSS"));
            LambdaUpdateWrapper<PBankSupportCfg> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(PBankSupportCfg::getInstId, entity.getInstId());
            wrapper.eq(PBankSupportCfg::getDstChannelId, entity.getDstChannelId());
            wrapper.eq(PBankSupportCfg::getSupportBizType, entity.getSupportBizType());
            wrapper.eq(PBankSupportCfg::getBankNo, entity.getBankNo());
            pBankSupportCfgService.update(db,wrapper);
        }
    }
}
